/*
* @Author: srain
* @Date:   2018-07-08 15:28:19
* @Last Modified by:   Srain 
* @Last Modified time: 2018-09-12 15:56:24
*/

var webpack = require('webpack');  
var ExtractTextPlugin = require("extract-text-webpack-plugin");  
const HtmlWebpackPlugin = require('html-webpack-plugin');

// 环境变量配置，dev / online
var WEBPACK_ENV  = process.env.WEBPACK_ENV || 'dev';

// 获取html-webpack-plugin参数的方法 
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/' + name + '.html',  //原来基础的模板
        filename    : 'view/' + name + '.html',  //目标文件
        title       : title,    
        inject      : true,    //所有JavaScript资源将被放置在正文元素的底部
        hash        : true,    //有包含的脚本和CSS文件附加一个独特的编译哈希。这对缓存清除非常有用
        chunks      : ['common', name]  //允许你只添加一些块（例如只有单元测试块）
    };
};

var config = {
	entry: {
		'common': ['./src/page/common/index.js'],//公共js模块  
		'index' : ['./src/page/index/index.js'],
    'user-login' : ['./src/page/user-login/index.js'],
    'user-pass-reset' : ['./src/page/user-pass-reset/index.js'],
    'user-center'       : ['./src/page/user-center/index.js'],
    'user-center-update': ['./src/page/user-center-update/index.js'],
    'user-pass-update'  : ['./src/page/user-pass-update/index.js'],
    'user-score'        : ['./src/page/user-score/index.js'],
    'user-group'        : ['./src/page/user-group/index.js'],
    'user-project'        : ['./src/page/user-project/index.js'],
    'user-credit'        : ['./src/page/user-credit/index.js'],
    'new'   : ['./src/page/new/index.js'],
    'new-detail'   : ['./src/page/new-detail/index.js'],
    'project'  : ['./src/page/project/index.js'],
    'project-detail'  : ['./src/page/project-detail/index.js'],
    'volunteer'  : ['./src/page/volunteer/index.js']
	},
	output: {
		path : __dirname + '/dist/',
    publicPath : '/dist/',
		filename: 'js/[name].js'
	},
	externals:{  
		'jquery':'window.jQuery'  
	},
  devServer: {
        inline: true,
        port: 8081
    },
	module: {  
		loaders: [  
      { test: /\.css$/, loader:ExtractTextPlugin.extract("style-loader","css-loader")},//css单独打包配置，安装ExtractTextPlugin  
      { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=100&name=resource/[name].[ext]' },
      { test: /\.string$/, loader: 'html-loader'}
      ]  
    },  
    resolve : {
      alias : {
        node_modules    : __dirname + '/node_modules',
        util            : __dirname + '/src/util',
        page            : __dirname + '/src/page',
        service         : __dirname + '/src/service',
        image           : __dirname + '/src/image'
      }
    },
    plugins: [  
    //独立通用模块到js/base.js
    new webpack.optimize.CommonsChunkPlugin({  
    name: 'common',//公共js模块的名字，赐给filename的base.js  
    filename:'js/base.js'  
  }),  
  //单独打包css，name为跟随文件命名  
	new ExtractTextPlugin("css/[name].css"),
  //html模板的处理
	new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
	new HtmlWebpackPlugin(getHtmlConfig('user-login','用户登录')),
  new HtmlWebpackPlugin(getHtmlConfig('user-pass-reset', '找回密码')),
  new HtmlWebpackPlugin(getHtmlConfig('user-center', '用户中心')),
  new HtmlWebpackPlugin(getHtmlConfig('user-center-update', '修改信息')),
  new HtmlWebpackPlugin(getHtmlConfig('user-pass-update', '修改密码')),
  new HtmlWebpackPlugin(getHtmlConfig('user-score', '我的评分')),
  new HtmlWebpackPlugin(getHtmlConfig('user-group', '我的团体')),
  new HtmlWebpackPlugin(getHtmlConfig('user-project', '我的活动')),
  new HtmlWebpackPlugin(getHtmlConfig('user-credit', '我的学分')),
  new HtmlWebpackPlugin(getHtmlConfig('new', '新闻动态')),
  new HtmlWebpackPlugin(getHtmlConfig('new-detail', '新闻详情')),
  new HtmlWebpackPlugin(getHtmlConfig('project', '活动列表页')),
  new HtmlWebpackPlugin(getHtmlConfig('project-detail', '活动详情')),
  new HtmlWebpackPlugin(getHtmlConfig('volunteer', '志愿者风采'))
	]
};

// if('dev' === WEBPACK_ENV){
//    // config.entry.common.push('webpack-dev-server/client?http://localhost:8081/');
// }

module.exports = config;